package cn.com.open.itembank.dto;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;

/**
 * Created by lucky on 2017/12/19.
 */
public class ResourceResponse extends CustomerResponse {

    /**
     * The Payload.
     */
    Object payload;
    /**
     * The Status.
     */
    Integer status;

    @Override
    public boolean parse() {
        HashMap map = new Gson().fromJson(getBody(), new TypeToken<HashMap>() {
        }.getType());
        if (map != null) {
            setSuccess((Boolean) map.get("success"));
            setError((String) map.get("error"));
            setMessage((String) map.get("message"));
            setPayload(map.get("payload"));
            return true;
        }
        setSuccess(false);
        return false;
    }

    /**
     * Init resource response.
     *
     * @param jsonStr the json str
     * @return the resource response
     */
    public static ResourceResponse init(String jsonStr) {
        HashMap map = new Gson().fromJson(jsonStr, new TypeToken<HashMap>() {
        }.getType());
        ResourceResponse resourceResponse = new ResourceResponse();
        resourceResponse.setSuccess((Boolean) map.get("success"));
        resourceResponse.setError((String) map.get("error"));
        resourceResponse.setMessage((String) map.get("message"));
        resourceResponse.setPayload(map.get("payload"));
        resourceResponse.setBodyContent(jsonStr);
        return resourceResponse;
    }

    /**
     * Gets payload.
     *
     * @return the payload
     */
    public Object getPayload() {
        return payload;
    }

    /**
     * Gets status.
     *
     * @return the status
     */
    public Integer getStatus() {
        return status;
    }

    /**
     * Sets status.
     *
     * @param status the status
     */
    public void setStatus(Integer status) {
        this.status = status;
    }

    /**
     * Sets payload.
     *
     * @param payload the payload
     */
    public void setPayload(Object payload) {
        this.payload = payload;
    }
}